// /web/mineronline?date=2021-07-05&day=1 7 30
// YYYY-MM-DD, h:mm:ss a
import service from '@/utils/http';
import { ApiBase } from '@/types/api/apiBase';
import { OnlineMonitorModel } from '@/types/monitor';

export function fetchOnlineMiner(
  date = '2021-07-05',
  day = 1,
  mid?: number,
): Promise<{
  originList: OnlineMonitorModel[];
  xAxis: number[];
}> {
  return new Promise(async (resolve, reject) => {
    const res = await service.get<ApiBase<{ list: OnlineMonitorModel[] }>>(
      '/web/mineronline',
      {
        method: 'GET',
        params: {
          date,
          day,
          mid,
        },
      },
    );
    if (res && res.data && res.data.code === 0) {
      const originList = res.data.result.list;

      const xAxis = originList
        .filter((m) => m.type === 'online')
        .reduce<number[]>((memo, cur) => {
          memo.push(cur.time);
          return memo;
        }, []);

      resolve({
        originList,
        xAxis,
      });
    } else {
      reject();
    }
  });
}
